/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BaseView.java
 *
 * Created on 20 déc. 2012, 18:08:55
 */
package polybook.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import polybook.MainController;
import polybook.model.NetworkTabLinked;
import polybook.model.Person;
import polybook.utils.Log;

/**
 *
 * @author Arnaud
 */
public class BaseView extends NamedJPanel {

    protected static final String title = "Accueil";
    public InfosProgressView timeProgress;

    /**
     * Creates new form BaseView
     */
    public BaseView() {
        initComponents();
        
        // Création d'un "petit" réseau initial	49	
        try {
            MainController.getInstance().setCurrentNetwork(new NetworkTabLinked(20));
        } catch (IOException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        // Gestion du panel datasTable
        MainController controller = MainController.getInstance();
        ArrayList<Person> personList = controller.getCurrentNetwork().getContainer();
        PersonJTableModel currentDatasModel = new PersonJTableModel(personList);
        controller.setCurrentTable(datasTable);
        controller.setCurrentBaseView(this);
        this.datasTable.setModel(currentDatasModel);
        currentDatasModel.fireTableDataChanged();

        // Gestion du panel datasPanel
        NamedJPanel menuView = new MenuView();
        menuView.includeIn(this.datasPanel);

        // Gestion du panel infosPanel
        NamedJPanel infosDefaultView = new InfosDefaultView(controller.getCurrentNetwork());
        infosDefaultView.includeIn(this.infosPanel);

        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        datasTable = new javax.swing.JTable();
        datasPanel = new javax.swing.JPanel();
        titlePolybook = new javax.swing.JLabel();
        infosPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btnDegreSort = new javax.swing.JButton();
        btnNameSort = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        btnDegreSlowSort = new javax.swing.JButton();
        btnNameSortSlow = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        buttonShuffle = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(900, 750));

        datasTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        datasTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        datasTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                datasTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(datasTable);

        javax.swing.GroupLayout datasPanelLayout = new javax.swing.GroupLayout(datasPanel);
        datasPanel.setLayout(datasPanelLayout);
        datasPanelLayout.setHorizontalGroup(
            datasPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 250, Short.MAX_VALUE)
        );
        datasPanelLayout.setVerticalGroup(
            datasPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 561, Short.MAX_VALUE)
        );

        titlePolybook.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        titlePolybook.setIcon(new javax.swing.ImageIcon(getClass().getResource("/polybook/images/title_polybook.png"))); // NOI18N
        titlePolybook.setPreferredSize(new java.awt.Dimension(250, 50));

        infosPanel.setPreferredSize(new java.awt.Dimension(0, 105));

        javax.swing.GroupLayout infosPanelLayout = new javax.swing.GroupLayout(infosPanel);
        infosPanel.setLayout(infosPanelLayout);
        infosPanelLayout.setHorizontalGroup(
            infosPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 250, Short.MAX_VALUE)
        );
        infosPanelLayout.setVerticalGroup(
            infosPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 105, Short.MAX_VALUE)
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Trie QuickSort");

        btnDegreSort.setText("Trier par degrés");
        btnDegreSort.setMaximumSize(new java.awt.Dimension(180, 23));
        btnDegreSort.setMinimumSize(new java.awt.Dimension(180, 23));
        btnDegreSort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDegreSortActionPerformed(evt);
            }
        });

        btnNameSort.setText("Trier la liste par Nom/Prénom");
        btnNameSort.setPreferredSize(new java.awt.Dimension(180, 23));
        btnNameSort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNameSortActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Trie à bulle");

        btnDegreSlowSort.setText("Trier la liste par degrés");
        btnDegreSlowSort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDegreSlowSortActionPerformed(evt);
            }
        });

        btnNameSortSlow.setText("Trier la liste par Nom/Prénom");
        btnNameSortSlow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNameSortSlowActionPerformed(evt);
            }
        });

        jLabel3.setText("Powered by Polytech's students");
        jLabel3.setEnabled(false);

        buttonShuffle.setText("Mélanger la liste");
        buttonShuffle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonShuffleActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Mélanger");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btnDegreSort, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnNameSort, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnNameSortSlow, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDegreSlowSort, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel3))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(buttonShuffle, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDegreSort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDegreSlowSort)
                    .addComponent(buttonShuffle))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNameSort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNameSortSlow))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel3))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(datasPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(infosPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(titlePolybook, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(titlePolybook, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(datasPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(infosPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnNameSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNameSortActionPerformed
        //Controller
        MainController controller = MainController.getInstance();
        Log.v("Debut TRI RAPIDE NOM");
        //Tri Quicksort par nom
        ((PersonJTableModel) controller.getCurrentTable().getModel()).quickNameSort();
        Log.v("Fin TRI RAPIDE NOM");
    }//GEN-LAST:event_btnNameSortActionPerformed

    private void btnDegreSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDegreSortActionPerformed
        //Controller
        MainController controller = MainController.getInstance();
        Log.v("Debut TRI RAPIDE Degré");
        //Tri Quicksort par nom
        ((PersonJTableModel) controller.getCurrentTable().getModel()).quickFriendSort();
        Log.v("Fin TRI RAPIDE Dégré");
    }//GEN-LAST:event_btnDegreSortActionPerformed

    private void datasTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_datasTableMouseClicked
        int selectedRow = this.datasTable.getSelectedRow();
        Person selectedPerson = ((PersonJTableModel) this.datasTable.getModel()).getData().get(selectedRow);
        DataPersonView personView = new DataPersonView(selectedPerson);
        personView.includeIn(datasPanel);
    }//GEN-LAST:event_datasTableMouseClicked

    private void btnDegreSlowSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDegreSlowSortActionPerformed
        //Controller
        MainController controller = MainController.getInstance();
        Log.v("Debut TRI LENT Degré");
        //Tri Quicksort par nom
        ((PersonJTableModel) controller.getCurrentTable().getModel()).slowFriendSort();
        Log.v("Fin TRI LENT Dégré");
    }//GEN-LAST:event_btnDegreSlowSortActionPerformed

    private void btnNameSortSlowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNameSortSlowActionPerformed
        //Controller
        MainController controller = MainController.getInstance();
        Log.v("Debut TRI RAPIDE NOM");
        //Tri Quicksort par nom
        ((PersonJTableModel) controller.getCurrentTable().getModel()).slowNameSort();
        Log.v("Fin TRI RAPIDE NOM");
    }//GEN-LAST:event_btnNameSortSlowActionPerformed

    private void buttonShuffleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonShuffleActionPerformed
        MainController controller = MainController.getInstance();
        ((PersonJTableModel) controller.getCurrentTable().getModel()).shuffleDatas();
    }//GEN-LAST:event_buttonShuffleActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDegreSlowSort;
    private javax.swing.JButton btnDegreSort;
    private javax.swing.JButton btnNameSort;
    private javax.swing.JButton btnNameSortSlow;
    private javax.swing.JButton buttonShuffle;
    private javax.swing.JPanel datasPanel;
    private javax.swing.JTable datasTable;
    private javax.swing.JPanel infosPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel titlePolybook;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getTitle() {
        return title;
    }

    public JPanel getInfosPanel() {
        return infosPanel;
    }
    
    public JPanel getDatasPanel() {
        return this.datasPanel;
    }
}
